// 此页面为数据目录层 简单get 请求 获取缓存数据至数据库

let _ = require("lodash");
import { contents_insert } from "../../db/insert";
import { gets } from "../../fetch/get";
let fs = require("fs");
export async function ontents_item({ uri, path, typeCode, maxTotal, category }) {

  let lastPage = false; //标识符 是否到最后一页
  let callback = function (res) {

    if (!res.data.length) {
      lastPage = true;
      return true;
    }
    _.forEach(res.data, item => {
      let title = [],
        updateValue = [],
        videoLink = [];
      _.forEach(item, (value, key) => {
        title.push(`${_.snakeCase(key)}`); //转换大小写
        updateValue.push(`'${value}'`);
      });
      title.push(`${_.snakeCase("category")}`); //转换大小写

      // category
      videoLink.push(`'${item.videoLink}'`);
      updateValue.push(`'${category}'`);

      contents_insert(title, updateValue, videoLink);
      // console.log("一次一致性")
    });


    // fs.writeFileSync("./九州缥缈录.json", JSON.stringify(res), "utf-8");
  };
  let i = 0;
  while (i < maxTotal) {

    if (lastPage) {
      i = maxTotal
      lastPage = !lastPage;
    }
    i++;
    let pam = path + `?c=${typeCode}&type=show&p=${i}`;
    let options = {
      hostname: uri,
      port: 443,
      path: pam,
      method: "GET"
    };
    gets(options, callback);
  }
}
